#!/bin/bash

port="9092"
zookeeperPort="2181"
kafkaName="kafka"
zookeeperName="zookeeper"


usage_exit() {
    printf "$usage"
    exit
}

# if [ $# -gt 3 ]; then
#     usage_exit
# fi

while [ $# -ge 1 ]; do
case $1 in
    -p | --port)
        [ -z "$2" ] && usage_exit
        port=$2
        shift
        ;;
    -zp | --zookeeperPort)
        [ -z "$2" ] && usage_exit
        zookeeperPort=$2
        shift
        ;;
    *)
        usage_exit
esac
shift
done

kafkaName=$kafkaName"_"$port
zookeeperName=$zookeeperName"_"$zookeeperPort

# set -x
echo "stop and remove kafka and zookeeper container"
echo "kafkaName: $kafkaName, zookeeperName: $zookeeperName"
zookeeper_ids=`docker ps -aq --filter name="${zookeeperName}"`
if [ 0"$zookeeper_ids" != "0" ]; then
    docker stop $(docker ps -aq --filter name="${zookeeperName}")
    docker rm $(docker ps -aq --filter name="${zookeeperName}")
fi

kafka_ids=`docker ps -aq --filter name="${kafkaName}"`
if [ 0"$kafka_ids" != "0" ]; then
    docker stop $(docker ps -aq --filter name="${kafkaName}")
    docker rm $(docker ps -aq --filter name="${kafkaName}")
fi
